// pages/mall-detail/mall-detail.js

import MallService from "../../class/service/MallService";
import Router from "../../class/Router";
import Tips from "../../utils/Tips";
var WxParse = require('../../wxParse/wxParse.js');

const mallService = new MallService();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: {
      bottom: true
    },
    totalPrice: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      id: options.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {
    
    var that = this;
    
    Tips.loading();
    
    mallService.detail(that.data.id).then(data => {
      Tips.loaded();
      console.log(data)
      that.setData({
        "init": true,
        "sku": data.sku,
        "courses": data.courses
      })

      var description = that.data.sku.description;
      /**
      * WxParse.wxParse(bindName , type, data, target,imagePadding)
      * 1.bindName绑定的数据名(必填)
      * 2.type可以为html或者md(必填)
      * 3.data为传入的具体数据(必填)
      * 4.target为Page对象,一般为this(必填)
      * 5.imagePadding为当图片自适应是左右的单一padding(默认为0,可选)
      */
      WxParse.wxParse('description', 'html', description, that, 5);

    }, res => {
      Tips.error("获取商品详情失败");
      that.setData({
        "init": true
      })
    })

    
  },

  selectItem: function(e) {
    const that = this;
    let selected = e.currentTarget.dataset.index
    let courses = that.data.courses;
    for (var i in courses) {
      if (courses[i].id === selected) {
        courses[i]['checked'] = courses[i]['checked'] === true ? false : true;
      }
    }
    that.setData({
      courses: courses
    })
    that.calulatePrice()
  },

  calulatePrice() {
    let that = this;
    let courses = that.data.courses;
    let totalPrice = 0;
    for (var i in courses) {
      if (courses[i].checked === true) {
        totalPrice = totalPrice + courses[i].price;
      }
    }
    that.setData({
      totalPrice: totalPrice
    })
  },

  // 此处开始下单，未支付状态
  goSubmit() {
    let that = this;
    // start generate url with courses & skus
    let selected = []
    let courses = that.data.courses;
    for (var i in courses) {
      if (courses[i].checked === true) {
        selected.push(courses[i].id)
      }
    }
    debugger
    wx.navigateTo({
      url: '../../pages/trading/prepare/parepare?id=' + that.data.sku.id + '&courses=' + selected.join(',')
    })
  },

  onTransitionEnd() {
    console.log(`You can't see me 🌚`);
  },

  toggleBottomPopup() {
    this.toggle('bottom');
  },

  toggle(type) {
    this.setData({
      [`show.${type}`]: !this.data.show[type]
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})